import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/index'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
import 'animate.css/animate.min.css' //引入动画
import Pagination from '@/components/pagination.vue'
import '@/assets/css/common.css'
import echarts from 'echarts'  //引入echarts


Vue.prototype.$echarts = echarts  //挂载在全局
Vue.config.productionTip = false
Vue.use(ElementUI)
//  每次切换页面时，调用进度条
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    //   //判断当前是否有登录的权限
    if (window.localStorage.getItem('stoken') === null) {
      next({
        path: '/loginForm',
      })
    }
  }
  if (to.path === '/login') {
    next({
      path: '/loginForm',
    })
  }
  // 最后写了一个next放行函数，才会对自己没有在路由导航中限制的路径放行
  if (to.path === '/HomePage' && JSON.parse(window.localStorage.getItem('roles')) == '顾客') {
    if (from.path !== '/MallHome') {
      next({
        path: '/MallHome',
      })
    } else {
      router.go(0)
    }
  } else if (to.path === '/MallHome' && JSON.parse(window.localStorage.getItem('roles')) !== '顾客' && JSON.parse(window.localStorage.getItem('roles')) !== null) {
    if (from.path !== '/HomePage') {
      next({
        path: '/HomePage',
      })
    } else {
      router.go(0)
    }
  } else {
    next() // 确保一定要调用 next()
  }
});
// 全局信息提示框
Vue.prototype.showMessage = function (type, message) {
  this.$message({
    message: message,
    showClose: true,
    duration: 1500,
    type: type,
    offset: 100
  });
}
// 全局分页组件挂载
Vue.component('Pagination', Pagination)
// 全局生成商品编号
Vue.prototype.$getDateNow = function () {
  let time = new Date(); // 时间戳
  let year = String(time.getFullYear()); // 年
  let mouth = String(time.getMonth() + 1); // 月
  let day = String(time.getDate()); // 日
  let hours = String(time.getHours()); // 时
  if (hours.length < 2) {
    hours = '0' + hours
  }
  let minutes = String(time.getMinutes()); // 分
  if (minutes.length < 2) {
    minutes = '0' + minutes
  }
  let seconds = String(time.getSeconds()); // 秒
  if (seconds.length < 2) {
    seconds = '0' + seconds
  }
  return year + mouth + day + hours + minutes + seconds;
}

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
